package com.summer.web.v1.dao.impl;

import com.summer.common.lib.core.jpa.DelegatingEntityManager;
import com.summer.web.v1.dao.UserDao;
import com.summer.web.v1.pojo.User;

import javax.annotation.Resource;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UserDaoImpl implements UserDao {

    static Logger logger = Logger.getLogger(UserDaoImpl.class.getName());

    private static Consumer<Throwable> COMMON_EXCEPTION_HANDLER = e -> logger.log(Level.SEVERE, e.getMessage());

    @Resource(name = "bean/DelegatingEntityManager")
    private DelegatingEntityManager delegatingEntityManager;

    @Override
    public boolean insertUser(User user) {
        EntityTransaction transaction = delegatingEntityManager.getTransaction();
        try {
            transaction.begin();
            delegatingEntityManager.persist(user);
            transaction.commit();
        } catch (Exception e) {
            transaction.rollback();
            return false;
        }
        return user.getId() > 0L;
    }

    /**
     * 参考
     * https://blog.csdn.net/woshisunxinyuan/article/details/82807402?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control&dist_request_id=1328626.23380.16154545418004417&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.control
     * @return
     */
    @Override
    public List<User> selectAllUsers() {
        EntityTransaction transaction = delegatingEntityManager.getTransaction();
        try {
            transaction.begin();
            CriteriaBuilder cb = delegatingEntityManager.getCriteriaBuilder();
            CriteriaQuery<User> cq = cb.createQuery(User.class);
            Root<User> root = cq.from(User.class);
            TypedQuery<User> query = delegatingEntityManager.createQuery(cq);
            transaction.commit();
            return query.getResultList();
        } catch (Exception e) {
            transaction.rollback();
        }
        return null;
    }
/*

    public static void main(String[] args) {

        try {
            DerbyDBManager dbManager = DerbyDBManager.getInstance();

            Connection connection = dbManager.getConnection();
            boolean execute = connection.createStatement().execute(DerByEnums.DROP_USER_TABLE.sql);
            if (execute) {
                System.out.println("创建表成功：" + execute);
                connection.close();
            }

            dbManager.executeQuery(
                    "SELECT * FROM users",
                    resultSet ->
                    {
                        ResultSetMetaData metaData = resultSet.getMetaData();
                        System.out.println("当前表的名称：" + metaData.getTableName(1));
                        System.out.println("当前表的列个数：" + metaData.getColumnCount());
                        for (int i = 1; i <= metaData.getColumnCount(); i++) {
                            System.out.println("列名称：" + metaData.getColumnLabel(i) + ", 类型：" + metaData.getColumnClassName(i));
                        }

                        return 1;
                    },
                    COMMON_EXCEPTION_HANDLER
            );

            Connection connection1 = dbManager.getConnection();
            execute = connection1.createStatement().execute(DerByEnums.DROP_USER_TABLE.sql);
            if (execute) {
                System.out.println("删除表成功：" + execute);
                connection1.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
            logger.log(Level.SEVERE, "Exception: " + e.getMessage());
        }
    }*/
}
